﻿Imports CapaLogica.CL.Foro
Imports CapaLogica.CL.Usuario
Imports CapaLogica.CL.Tema

Public Class frmForoProfesor

    Private idProfesor As Integer
    Private idforo As Integer
    Private idTema As Integer

    Public Sub New(ByRef pIdProfesor As Integer)
        InitializeComponent()
        idProfesor = pIdProfesor
        listarForos()
        lblTitulo.Text = "Foros del Profesor " & Session.NombreCompleto
    End Sub



    Private Sub listarForos()
        Try
            dtgForos.DataSource = GestorForo.listarForosDeProfesorGrid(idProfesor)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try

    End Sub


    Private Sub tsbCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCancelar.Click
        Me.Dispose()
    End Sub


    Public Sub cargarForo()
        Try

            idForo = dtgForos.SelectedRows(0).Cells(0).Value

            Dim foro As Dictionary(Of String, Object) = GestorForo.consultarForo(idForo)
            lblNombre.Text = foro.Item("nombre")
            lblNumGrupo.Text = foro.Item("numGrupo")
            lblCurso.Text = foro.Item("curso")
            dgvTemas.DataSource = GestorTema.listarTemasDeUnForoGrid(idforo)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try


    End Sub

    Private Sub btnConsultarForo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultarForo.Click
        If dtgForos.CurrentRow Is Nothing Then
            MsgBox("Debe seleccionar un foro", MsgBoxStyle.Exclamation, "Error")
        Else
            cargarForo()
        End If
    End Sub

    Private Sub btnCrearTema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrearTema.Click
        If dtgForos.CurrentRow Is Nothing Then
            MsgBox("Debe seleccionar un foro", MsgBoxStyle.Exclamation, "Error")
        Else
            idforo = dtgForos.SelectedRows(0).Cells(0).Value
            Dim pantalla As frmRegistrarModificarTema = New frmRegistrarModificarTema(idforo, "Crear Tema", Me)
            Me.Visible = False
            pantalla.ShowDialog()
        End If
    End Sub

 

    Private Sub btnCerrarTema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCerrarTema.Click
        If dgvTemas.CurrentRow Is Nothing Then
            MsgBox("Debe seleccionar el tema que desea cerrar", MsgBoxStyle.Exclamation, "Error")
        Else

            Dim button As DialogResult = MessageBox.Show("¿Desea cerrar el Tema?",
                                                                           Me.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
            If button = DialogResult.Yes Then
                Try
                    GestorTema.CerrarTema(dgvTemas.SelectedRows(0).Cells(0).Value)
                    MsgBox("¡El tema ha sido cerrado!", MsgBoxStyle.Information, "Tema")
                    cargarForo()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
                End Try

            Else
                MsgBox("¡El tema no ha sido cerrado!", MsgBoxStyle.Information, "Tema")

            End If
        End If
    End Sub


    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If dgvTemas.CurrentRow Is Nothing Then
            MsgBox("Debe seleccionar el tema que desea eliminar", MsgBoxStyle.Exclamation, "Error")
        Else
            Dim button As DialogResult = MessageBox.Show("¿Desea eliminar el Tema? Si se elimina el tema se borran todos sus aportes. ",
                                                                              Me.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
            If button = DialogResult.Yes Then
                Try
                    GestorTema.BorrarTema(dgvTemas.SelectedRows(0).Cells(0).Value)
                    MsgBox("¡El tema ha sido eliminado!", MsgBoxStyle.Information, "Tema")
                    cargarForo()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
                End Try

            Else
                MsgBox("¡El tema no ha sido eliminado!", MsgBoxStyle.Information, "Tema")

            End If
        End If
    End Sub


    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        If ((dgvTemas.CurrentRow Is Nothing) OrElse (dgvTemas.SelectedRows.Count = 0)) Then
            MsgBox("Debe de seleccionar un curso antes de modificar", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else
            Dim id As Int32 = CInt(dgvTemas.SelectedRows(0).Cells(0).Value)
            Dim nombre As String = CStr(dgvTemas.SelectedRows(0).Cells(1).Value)
            Dim descripcion As String = CStr(dgvTemas.SelectedRows(0).Cells(2).Value)
            Dim fechaInicio As DateTime = (dgvTemas.SelectedRows(0).Cells(4).Value)
            Dim fechaFin As DateTime = (dgvTemas.SelectedRows(0).Cells(5).Value)
            Dim calificacion As Boolean = CBool(dgvTemas.SelectedRows(0).Cells(6).Value)
            Dim justificacion As String = CStr(dgvTemas.SelectedRows(0).Cells(8).Value)
            Dim cantAportes As Int32 = CInt(dgvTemas.SelectedRows(0).Cells(7).Value)

            Dim frmModificar As New frmModificarTema(id, nombre, descripcion, idForo, fechaInicio, fechaFin, calificacion, justificacion, cantAportes)
            frmModificar.ShowDialog()

        End If
    End Sub


    Private Sub btnRefrescar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefrescar.Click
        refrescar()
    End Sub

    Private Sub refrescar()
        lblNombre.Text = ""
        lblNumGrupo.Text = ""
        lblCurso.Text = ""
        dgvTemas.DataSource = Nothing

    End Sub

End Class